--体验擂台BOSS
--#include "data\config\exep\ExepArenaBossConfig.lua" once


--以下是副本数据
EXEPARENABOSS_FUBEN_KILLBOSS_NUM		= 0			--击杀BOSS数量

--[[
注册一些事件
]]
function ResisterExepArenaBossProc()
	--print("ResisterExepArenaBossProc...")
	SceneEnterDispatcher.register( ExepArenaBossConfig.sceneId, OnEnterExepArenaBoss )
	SceneHandExitDispatcher.register( ExepArenaBossConfig.sceneId,  OnCommonSceneHandExit )		--注册手工离开场景、副本时触发的事件（先执行）
end

--[[
已经进入体验魔尊战场、盟重杀神
]]
function OnEnterExepArenaBoss( sysarg, sceneId, lastSceneId )
	--print("OnEnterExepArenaBoss, sceneId="..sceneId)
	local sceneHandle = Actor.getSceneHandle(sysarg)
	SetExepArenaBossNpcVisible( sceneHandle )
	InitExepArenaBossPanel( sysarg )
end

--[[
进入擂台BOSS副本
]]
function EnterExepArenaBoss( sysarg )
	--[[
	因为 ArenaBossConfig.reloginType 为1，所以已经调用了Fuben.setOwnerId()设置了副本的ownerId
	]]
	CommonEnterScence( sysarg, ExepArenaBossConfig, 0, "", nil, nil )
end

function InitExepArenaBossPanel( sysarg )
	local contStyle 	= 4					--只显示剩余时间
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_ArenaBoss
	local title			= Lang.ScriptTips.ArenaBossName
	OpenCommonStylePanel( sysarg, 1, activityId, 0, title, nil, contStyle, buttons  )
end

--[[
挑战擂台BOSS（体验）
--如果changeLevel要求等级满足，免费召唤；否则扣除changeFee
callType:1-元宝召唤，2-道具召唤
]]
function challengeExepArenaBoss( sysarg, bossIdx, callType )
	--print("challengeExepArenaBoss, bossIdx="..bossIdx)
	local bossCfg = ExepArenaBossConfig.boss[bossIdx]
	if bossCfg then
		local fubenPtr 	  = Actor.getFubenPrt(sysarg)
		local fubenHandle = Fuben.getFubenHandle(fubenPtr)
		local sceneHandle = Actor.getSceneHandle(sysarg)
		if HasExepArenaBossInArena( fubenHandle ) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.ArenaBoss002, ttFlyTip)
			return
		end

		local consume = nil
		if callType == 1 then
			consume = bossCfg.callBossWithYuanBao
		elseif callType == 2 then
			consume = bossCfg.callBossWithItem
		else
			return
		end

		if not consume then
			return
		end

		if not CheckConsumeCond( sysarg, {consume}, 1, false ) then
			return
		end

		--扣除召唤费用
		local logId  = GameLog.clArenaBossCallFee
		local logStr = Lang.ScriptTips.ArenaBossLog01
		if not DoConsumeCond( sysarg, {consume}, 1, logId, logStr) then
			return
		end

		local monPtr = Fuben.createMonster( sceneHandle, bossCfg.boss.monsterId, bossCfg.boss.pos[1], bossCfg.boss.pos[2], bossCfg.boss.livetime)
		if monPtr then
			local npcPtr = Fuben.getFbNpcPtrById( sceneHandle, bossCfg.npcId )
			if npcPtr then
				Actor.setEntityVisibleFlag(npcPtr, false)
			end
		end
	end
end

--[[
判断擂台上是否已经有BOSS
]]
function HasExepArenaBossInArena( fubenHandle )
	for bossIdx, bossCfg in ipairs( ExepArenaBossConfig.boss ) do
		local num = System.getMonsterCountById( bossCfg.boss.bossId, bossCfg.boss.sceneId, fubenHandle )
		if num > 0 then
			return true
		end
	end
	return false
end


function GetExepArenaBossCfgByBossId( bossId )
	for bossIdx, bossCfg in ipairs( ExepArenaBossConfig.boss ) do
		if bossCfg.boss.monsterId == bossId then
			return bossCfg
		end
	end
	return nil
end

--[[
设置全部NPC为可见状态
]]
function SetExepArenaBossNpcVisible( sceneHandle )
	for bossIdx, bossCfg in ipairs( ExepArenaBossConfig.boss ) do
		local npcPtr = Fuben.getFbNpcPtrById( sceneHandle, bossCfg.npcId )
		if npcPtr then
			Actor.setEntityVisibleFlag(npcPtr, true)
		end
	end
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnExepArenaBossDie( monster, sysarg, monId, lastHitKiller )
	--print("OnExepArenaBossDie, ..monId="..monId)
	local fubenPtr = Actor.getFubenPrt( monster )
	if not fubenPtr then
		return
	end
	local bossCfg = GetExepArenaBossCfgByBossId( monId )
	if not bossCfg then
	    return
	end

	local killBossNum = Fuben.getFbValue( fubenPtr, EXEPARENABOSS_FUBEN_KILLBOSS_NUM )
	local newKillBossNum = killBossNum + 1
	--print("OnExepArenaBossDie, ..newKillBossNum="..newKillBossNum)
	if newKillBossNum >= #ExepArenaBossConfig.boss then
		Actor.postEntityScriptDelayMsg(sysarg, ExepArenaBossConfig.doneLeaveTime*1000, 0, 16,0,0,0,0,false)
		local boardStr = Lang.ScriptTips.COM0054				--注意：字符串中要带有一个 %d
		OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, ExepArenaBossConfig.doneLeaveTime )
	else
		Fuben.setFbValue( fubenPtr, EXEPARENABOSS_FUBEN_KILLBOSS_NUM, newKillBossNum)
	end
end

function GetExepArenaBossCfgByBossId( bossId )
	for bossIdx, bossCfg in ipairs( ExepArenaBossConfig.boss ) do
		if bossCfg.boss.monsterId == bossId then
			return bossCfg
		end
	end
	return nil
end



ResisterExepArenaBossProc()
